package pingu.reseau;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import pingu.reseau.messages.*;

public class PinguClient {
	
	private boolean ready;
	private ObjectOutputStream sortie_socket;
	private ObjectInputStream entree_socket;
	private Socket socket;
	private String playerName;

	public PinguClient(){
		this.ready=false;
	}
	public PinguClient(String playerName){
		this.ready=false;
		this.playerName= playerName;
	}
	
	public void go(int port) throws UnknownHostException, IOException{
		this.socket = new Socket("localhost",port);
		this.sortie_socket = new ObjectOutputStream(socket.getOutputStream());
		this.entree_socket = new ObjectInputStream(socket.getInputStream());
	}
	
	public void setReady(boolean ready) throws IOException{
		this.ready=ready;		
		this.sortie_socket.writeObject(new RDY(ready));
		this.sortie_socket.flush();
	}
		
	public static void main(String args[]) throws UnknownHostException, IOException, ClassNotFoundException{
		PinguClient pingu = new PinguClient("Mat");
		Scanner scanIn = new Scanner(System.in);
		System.out.println("Port du serveur :");
		String prt = scanIn.next();
		pingu.go(new Integer(prt));
		while(true){
			String nxtIn = scanIn.next();
			if("ready".equals(nxtIn)){
				pingu.setReady(true);
			}else if("notready".equals(nxtIn)){
				pingu.setReady(false);
			}
			Message msg = (Message)pingu.entree_socket.readObject();
			System.out.println(pingu.socket.getInetAddress()+":"+pingu.socket.getPort()+" - "+msg);
			if("MAP".equals(msg.getType())){
			}
		}
	}
}
